import { ref } from "vue";

export function useIntervalTaskHook(f: Function, ms: number,autoStart?:boolean,immediate?:boolean) {
  const taskId = ref<any>(null);
  if(autoStart){
    start()
  }
  
  function start() {
    if(immediate){
      f();
    }
    taskId.value = setInterval(() => {
      f();
    }, ms);
  }
  function stop() {
    if (taskId.value != null) {
      clearInterval(taskId.value);
      taskId.value = null;
    }
  }
  return {
    taskId,
    start,
    stop
  };
}
